/*
 * $Id: WdpMainWindow.java 25 2008-12-14 13:29:28Z rdubisz $
 */
package wdp;

import wdp.worker.WorkerEditFrame;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import wdp.project.CostEditFrame;
import wdp.project.ProjectAndTaskFrame;
import wdp.project.ProjectEditFrame;
import wdp.project.ProjectTreePane;
import wdp.project.TaskEditFrame;
import wdp.reports.ReportLauncher;
import wdp.settings.DbConnectionFrame;
import wdp.settings.SettingsEditFrame;
import wdp.worker.PostEditFrame;
import wdp.worker.TeamEditFrame;

/**
 * The application's main frame.
 */
public class WdpMainWindow extends FrameView {

    public WdpMainWindow(SingleFrameApplication app) {
        super(app);

        initComponents();

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {

            @Override
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String) (evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer) (evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });

        initMainPane();
    }

    public boolean isSaveNeeded() {
        return saveNeeded;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        jMenuItemWorkers = new javax.swing.JMenuItem();
        jMenuItemTeams = new javax.swing.JMenuItem();
        jMenuItemPosts = new javax.swing.JMenuItem();
        jMenuItemCosts = new javax.swing.JMenuItem();
        jMenuItemProjects = new javax.swing.JMenuItem();
        jMenuItemTasks = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        jMenuSettings = new javax.swing.JMenu();
        jMenuItemDbConfig = new javax.swing.JMenuItem();
        jMenuItemAdvancedSetings = new javax.swing.JMenuItem();
        jMenuTest = new javax.swing.JMenu();
        jMenuItemReportTest = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setName("mainPanel"); // NOI18N

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 558, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 290, Short.MAX_VALUE)
        );

        menuBar.setName("menuBar"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(wdp.WdpApp.class).getContext().getResourceMap(WdpMainWindow.class);
        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(wdp.WdpApp.class).getContext().getActionMap(WdpMainWindow.class, this);
        jMenuItemWorkers.setAction(actionMap.get("showWorkersWindow")); // NOI18N
        jMenuItemWorkers.setText(resourceMap.getString("jMenuItemWorkers.text")); // NOI18N
        jMenuItemWorkers.setName("jMenuItemWorkers"); // NOI18N
        fileMenu.add(jMenuItemWorkers);

        jMenuItemTeams.setAction(actionMap.get("showTeamsWindow")); // NOI18N
        jMenuItemTeams.setText(resourceMap.getString("jMenuItemTeams.text")); // NOI18N
        jMenuItemTeams.setName("jMenuItemTeams"); // NOI18N
        fileMenu.add(jMenuItemTeams);

        jMenuItemPosts.setAction(actionMap.get("showPostsWindow")); // NOI18N
        jMenuItemPosts.setText(resourceMap.getString("jMenuItemPosts.text")); // NOI18N
        jMenuItemPosts.setName("jMenuItemPosts"); // NOI18N
        fileMenu.add(jMenuItemPosts);

        jMenuItemCosts.setAction(actionMap.get("showCostsWindow")); // NOI18N
        jMenuItemCosts.setName("jMenuItemCosts"); // NOI18N
        fileMenu.add(jMenuItemCosts);

        jMenuItemProjects.setAction(actionMap.get("showProjectsWindow")); // NOI18N
        jMenuItemProjects.setText(resourceMap.getString("jMenuItemProjects.text")); // NOI18N
        jMenuItemProjects.setName("jMenuItemProjects"); // NOI18N
        fileMenu.add(jMenuItemProjects);

        jMenuItemTasks.setAction(actionMap.get("showTasksWindow")); // NOI18N
        jMenuItemTasks.setText(resourceMap.getString("jMenuItemTasks.text")); // NOI18N
        jMenuItemTasks.setName("jMenuItemTasks"); // NOI18N
        fileMenu.add(jMenuItemTasks);

        jSeparator2.setName("jSeparator2"); // NOI18N
        fileMenu.add(jSeparator2);

        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        jMenuSettings.setText(resourceMap.getString("jMenuSettings.text")); // NOI18N
        jMenuSettings.setName("jMenuSettings"); // NOI18N

        jMenuItemDbConfig.setAction(actionMap.get("editDbConnectionWindow")); // NOI18N
        jMenuItemDbConfig.setText(resourceMap.getString("jMenuItemDbConfig.text")); // NOI18N
        jMenuItemDbConfig.setName("jMenuItemDbConfig"); // NOI18N
        jMenuSettings.add(jMenuItemDbConfig);

        jMenuItemAdvancedSetings.setAction(actionMap.get("showAdvancedSettingsWindow")); // NOI18N
        jMenuItemAdvancedSetings.setText(resourceMap.getString("jMenuItemAdvancedSetings.text")); // NOI18N
        jMenuItemAdvancedSetings.setName("jMenuItemAdvancedSetings"); // NOI18N
        jMenuSettings.add(jMenuItemAdvancedSetings);

        menuBar.add(jMenuSettings);

        jMenuTest.setText(resourceMap.getString("jMenuTest.text")); // NOI18N
        jMenuTest.setName("jMenuTest"); // NOI18N

        jMenuItemReportTest.setText(resourceMap.getString("jMenuItemReportTest.text")); // NOI18N
        jMenuItemReportTest.setName("jMenuItemReportTest"); // NOI18N
        jMenuItemReportTest.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemReportTestActionPerformed(evt);
            }
        });
        jMenuTest.add(jMenuItemReportTest);

        menuBar.add(jMenuTest);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 558, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 384, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItemReportTestActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemReportTestActionPerformed
        ReportLauncher launcher = new ReportLauncher();
        launcher.run();
    }//GEN-LAST:event_jMenuItemReportTestActionPerformed

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = WdpApp.getApplication().getMainFrame();
            aboutBox = new WdpAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        WdpApp.getApplication().show(aboutBox);
    }

    @Action
    public void showTasksWindow() {
        JFrame frame = new JFrame();
        frame.setContentPane(new TaskEditFrame());
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(wdp.WdpApp.class).getContext().getResourceMap(WdpMainWindow.class);
        frame.setTitle(resourceMap.getString("showTasksWindow.Action.text"));
        frame.setIconImage(resourceMap.getImageIcon("showTasksWindow.Action.icon").getImage());
        frame.pack();
        frame.setVisible(true);
        frame.setLocationRelativeTo(WdpApp.getApplication().getMainFrame());
        WdpApp.getApplication().show(frame);
    }

    @Action
    public void showWorkersWindow() {
        JFrame frame = new JFrame();
        frame.setContentPane(new WorkerEditFrame());
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(wdp.WdpApp.class).getContext().getResourceMap(WdpMainWindow.class);
        frame.setTitle(resourceMap.getString("showWorkersWindow.Action.text"));
        frame.setIconImage(resourceMap.getImageIcon("showWorkersWindow.Action.icon").getImage());
        frame.pack();
        frame.setVisible(true);
        frame.setLocationRelativeTo(WdpApp.getApplication().getMainFrame());
        WdpApp.getApplication().show(frame);
    }

    @Action
    public void showTeamsWindow() {
        JFrame frame = new JFrame();
        frame.setContentPane(new TeamEditFrame());
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(wdp.WdpApp.class).getContext().getResourceMap(WdpMainWindow.class);
        frame.setTitle(resourceMap.getString("showTeamsWindow.Action.text"));
        frame.setIconImage(resourceMap.getImageIcon("showTeamsWindow.Action.icon").getImage());
        frame.pack();
        frame.setVisible(true);
        frame.setLocationRelativeTo(WdpApp.getApplication().getMainFrame());
        WdpApp.getApplication().show(frame);
    }

    @Action
    public void showPostsWindow() {
        JFrame frame = new JFrame();
        frame.setContentPane(new PostEditFrame());
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(wdp.WdpApp.class).getContext().getResourceMap(WdpMainWindow.class);
        frame.setTitle(resourceMap.getString("showPostsWindow.Action.text"));
        frame.setIconImage(resourceMap.getImageIcon("showPostsWindow.Action.icon").getImage());
        frame.pack();
        frame.setVisible(true);
        frame.setLocationRelativeTo(WdpApp.getApplication().getMainFrame());
        WdpApp.getApplication().show(frame);
    }

    @Action
    public void showCostsWindow() {
        JFrame frame = new JFrame();
        frame.setContentPane(new CostEditFrame());
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(wdp.WdpApp.class).getContext().getResourceMap(WdpMainWindow.class);
        frame.setTitle(resourceMap.getString("showCostsWindow.Action.text"));
        frame.setIconImage(resourceMap.getImageIcon("showCostsWindow.Action.icon").getImage());
        frame.pack();
        frame.setVisible(true);
        frame.setLocationRelativeTo(WdpApp.getApplication().getMainFrame());
        WdpApp.getApplication().show(frame);
    }

    @Action
    public void showProjectsWindow() {
        JFrame frame = new JFrame();
        frame.setContentPane(new ProjectEditFrame());
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(wdp.WdpApp.class).getContext().getResourceMap(WdpMainWindow.class);
        frame.setTitle(resourceMap.getString("showProjectsWindow.Action.text"));
        frame.setIconImage(resourceMap.getImageIcon("showProjectsWindow.Action.icon").getImage());
        frame.pack();
        frame.setVisible(true);
        frame.setLocationRelativeTo(WdpApp.getApplication().getMainFrame());
        WdpApp.getApplication().show(frame);
    }

    @Action
    public void showAdvancedSettingsWindow() {
        JFrame frame = new JFrame();
        frame.setContentPane(new SettingsEditFrame());
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(wdp.WdpApp.class).getContext().getResourceMap(WdpMainWindow.class);
        frame.setTitle(resourceMap.getString("showAdvancedSettingsWindow.Action.text"));
        frame.setIconImage(resourceMap.getImageIcon("showAdvancedSettingsWindow.Action.icon").getImage());
        frame.pack();
        frame.setVisible(true);
        frame.setLocationRelativeTo(WdpApp.getApplication().getMainFrame());
        WdpApp.getApplication().show(frame);
    }

    @Action
    public void showProjectTree() {
        JFrame frame = new JFrame();
        frame.setContentPane(new ProjectTreePane());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    @Action
    public void editDbConnectionWindow() {
        JFrame frame = new JFrame();
        frame.setContentPane(new DbConnectionFrame());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem jMenuItemAdvancedSetings;
    private javax.swing.JMenuItem jMenuItemCosts;
    private javax.swing.JMenuItem jMenuItemDbConfig;
    private javax.swing.JMenuItem jMenuItemPosts;
    private javax.swing.JMenuItem jMenuItemProjects;
    private javax.swing.JMenuItem jMenuItemReportTest;
    private javax.swing.JMenuItem jMenuItemTasks;
    private javax.swing.JMenuItem jMenuItemTeams;
    private javax.swing.JMenuItem jMenuItemWorkers;
    private javax.swing.JMenu jMenuSettings;
    private javax.swing.JMenu jMenuTest;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables
    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;
    private JDialog aboutBox;
    private boolean saveNeeded;

    private void initMainPane() {
        testDbConnection();
        ProjectAndTaskFrame projectAndTaskFrame = new ProjectAndTaskFrame();
        setComponent(projectAndTaskFrame);
    }

    private void testDbConnection() {
        try{
        javax.persistence.EntityManager entityManager = 
                javax.persistence.Persistence.createEntityManagerFactory("db.fdbPU").createEntityManager();
        } catch (Exception ex) {
            JFrame frame = new JFrame();
            frame.setContentPane(new DbConnectionFrame());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        }
    }

}
